package com.glorysoft.xztpda.ui.showfile

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.databinding.ActivityShowFileBinding
import com.glorysoft.xztpda.ui.WebActivity
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import kotlinx.android.synthetic.main.activity_lot_query.*
import kotlinx.android.synthetic.main.activity_show_file.*
import kotlinx.android.synthetic.main.activity_show_file.etLotID
import kotlinx.android.synthetic.main.activity_show_file.ivLotScan

/**
 * FileName: ShowFileActivity
 * Author: XJH
 * Date: 2022/4/13 17:33
 * Description:
 * History:
 */
class ShowFileActivity : BaseActivity<ShowFileViewModel,ActivityShowFileBinding>(),
    View.OnClickListener {
    override fun layoutId(): Int {
        return R.layout.activity_show_file
    }

    override fun initVariableId(): Int {
        return BR.showFileViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("文件查看")
        initEdit(etLotID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.getShowFile(s)
            }

        })

        viewModel.defUI.callObserve.observe(this){
            mBinding.setVariable(BR.showFileViewModel,viewModel)
        }
    }

    override fun initData() {
        tvPackage.setOnClickListener(this)
        tvPrint.setOnClickListener(this)
        tvWeld.setOnClickListener(this)
        tvTest.setOnClickListener(this)
        ivLotScan.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        val bundle = Bundle()
        when(v?.id){
            R.id.tvPackage ->{
                bundle.putString("PATH",viewModel.lotInfoEntity?.packagecs)
                startActivity(WebActivity::class.java,bundle)
            }
            R.id.tvPrint ->{
                bundle.putString("PATH",viewModel.lotInfoEntity?.printingcs)
                startActivity(WebActivity::class.java,bundle)
            }
            R.id.tvWeld ->{
                bundle.putString("PATH",viewModel.lotInfoEntity?.weldingwirecs)
                startActivity(WebActivity::class.java,bundle)
            }
            R.id.tvTest ->{
                bundle.putString("PATH",viewModel.lotInfoEntity?.testcs)
                startActivity(WebActivity::class.java,bundle)
            }
            R.id.ivLotScan ->{
                startScanForCode(this,10001)
            }
        }


    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        // 当扫码页面结束后，处理扫码结果。
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK || data == null) {
            return
        }
        if (resultCode == RESULT_OK){
            // 从onActivityResult返回data中，用ScanUtil.RESULT作为key值取到HmsScan返回值。
            when (val obj: Any = data.getParcelableExtra(ScanUtil.RESULT)) {
                is HmsScan -> {
                    val barCode = obj.getOriginalValue()
                    when(requestCode){
                        10001 -> {
                            etLotID.setText(barCode)
                            viewModel.getShowFile(barCode)
                        }
                    }

                }
            }
        }
    }
}